package org.amphiprion.diceroller.entity;

import java.util.ArrayList;
import java.util.List;

public class DiceDefinition {
	private String id;
	private float[] normalForResult;
	private List<DiceFaceDefinition> faceDefinitions = new ArrayList<DiceFaceDefinition>();

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public List<DiceFaceDefinition> getFaceDefinitions() {
		return faceDefinitions;
	}

	public DiceFaceDefinition getFaceDefinition(String faceNumber) {
		for (DiceFaceDefinition dfd : faceDefinitions) {
			if (dfd.getNumber().equals(faceNumber)) {
				return dfd;
			}
		}
		return null;
	}

	public void setFaceDefinitions(List<DiceFaceDefinition> faceDefinitions) {
		this.faceDefinitions = faceDefinitions;
	}

	public DiceFaceDefinition getNextFaceDefinition(String faceNumber, boolean allowLoop) {
		boolean mustTakeNext = false;
		for (DiceFaceDefinition dfd : faceDefinitions) {
			if (mustTakeNext) {
				mustTakeNext = false;
				return dfd;
			}
			if (dfd.getNumber().equals(faceNumber)) {
				mustTakeNext = true;
			}
		}
		if (mustTakeNext && faceDefinitions.size() > 0) {
			if (allowLoop) {
				return faceDefinitions.get(0);
			} else {
				return faceDefinitions.get(faceDefinitions.size() - 1);
			}
		} else {
			return null;
		}
	}

	public DiceFaceDefinition getPreviousFaceDefinition(String faceNumber, boolean allowLoop) {
		DiceFaceDefinition result = null;
		for (DiceFaceDefinition dfd : faceDefinitions) {
			if (dfd.getNumber().equals(faceNumber)) {
				if (result != null) {
					return result;
				} else {
					if (allowLoop) {
						return faceDefinitions.get(faceDefinitions.size() - 1);
					} else {
						return faceDefinitions.get(0);
					}
				}
			} else {
				result = dfd;
			}
		}
		return null;
	}

	public float[] getNormalForResult() {
		return normalForResult;
	}

	public void setNormalForResult(float[] normalForResult) {
		this.normalForResult = normalForResult;
	}

}
